package com.cdut.hwadeefsc.controller;

import com.cdut.hwadeefsc.entity.scores;
import com.cdut.hwadeefsc.service.scoresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/students")
public class scoresController {

    @Autowired
    private scoresService scoresService;

    // 查看学生成绩
    @GetMapping("/{studentId}/grades")
    public ResponseEntity<List<scores>> getStudentGrades(
            @PathVariable Long studentId,
            @RequestParam(required = false) String semester) {
        try {
            List<scores> scores;
            if (semester != null && !semester.isEmpty()) {
                scores = scoresService.getStudentScoresBySemester(studentId, semester);
            } else {
                scores = scoresService.getStudentScores(studentId);
            }
            return ResponseEntity.ok(scores);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 录入成绩（老师权限）
    @PostMapping("/grades")
    public ResponseEntity<?> addGrades(@RequestBody scores score) {
        try {
            boolean success = scoresService.addScore(score);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 批量录入成绩
    @PostMapping("/grades/batch")
    public ResponseEntity<?> batchAddGrades(@RequestBody List<scores> scoreList) {
        try {
            boolean success = scoresService.batchAddScores(scoreList);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}
